package com.zm.blog.handle;

import com.zm.blog.constants.CommonConst;
import com.zm.blog.dao.HistoryInfoMapper;
import com.zm.blog.utils.cache.BlogCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @Author: Sister Xiao Ming
 * @Description:
 */
@Slf4j
@Component
@EnableScheduling
public class ScheduleTask {

    @Autowired
    private HistoryInfoMapper historyInfoMapper;

    @Scheduled(cron = "0 0 0 * * ?")
        public void cleanIpHistory() {
            CopyOnWriteArraySet<String> ipHistory = (CopyOnWriteArraySet<String>) BlogCache.get(CommonConst.IP_HISTORY);
            if (ipHistory == null) {
                ipHistory = new CopyOnWriteArraySet<>();
                BlogCache.put(CommonConst.IP_HISTORY, ipHistory);
            }
            ipHistory.clear();

            BlogCache.remove(CommonConst.IP_HISTORY_STATISTICS);
            Map<String, Object> history = new HashMap<>();
            history.put(CommonConst.IP_HISTORY_PROVINCE, historyInfoMapper.getHistoryByProvince());
            history.put(CommonConst.IP_HISTORY_IP, historyInfoMapper.getHistoryByIp());
            history.put(CommonConst.IP_HISTORY_HOUR, historyInfoMapper.getHistoryBy24Hour());
            history.put(CommonConst.IP_HISTORY_COUNT, historyInfoMapper.getHistoryCount());
            BlogCache.put(CommonConst.IP_HISTORY_STATISTICS, history);
        }
    

}
